package com.tanglon.Task;

import com.tanglon.constants.SystemConstants;
import com.tanglon.domain.entity.Article;
import com.tanglon.mapper.ArticleMapper;
import com.tanglon.service.ArticleService;
import com.tanglon.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class UpdateViewCountTask {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ArticleMapper articleMapper;

    @Scheduled(cron = "0 0/5 * * * ?")
    public void updateViewCount() {
        //查询redis的浏览量
        Map<String, Integer> cacheMap = redisCache.getCacheMap(SystemConstants.ARTICLE_VIEWCOUNT);
        //把浏览量更新到数据库中
        System.out.println("更新浏览量："+cacheMap);
        for (Map.Entry<String, Integer> entry : cacheMap.entrySet()) {
            Long articleId = Long.parseLong(entry.getKey());
            Long viewCount = Long.parseLong(entry.getValue().toString());
            articleMapper.updateViewCount(articleId,viewCount);
        }

    }
}
